(function($) {
	var pullPage = {start:0,pulling:false};
	var pull;
	var limit = 20;
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}

	function baseSetup() {
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var pull = $('#scroll0').pullToRefresh({
			up: {
				callback: function() {
					requestData()
				}
			}
		});
		pullPage.pullObj = pull;
		// 点击已读此条
		$('#OA_task_1').on('tap', '.mui-btn', function(event) {
			var elem = this;
			var li = elem.parentNode.parentNode;
			
		});
		requestData();
	}
	
	function requestData(){
		if (pullPage.pulling && pullPage.pullObj) {
			pullPage.pulling = false //结束刷新
			pullPage.pullObj.endPullUpToRefresh()
			return;
		}
		pullPage.pulling = true //开始刷新了
		var method = 'web.INMPlatform:FindTrackMessageByUser';
		const user = app.getState();
		var arg = {
			parr: 'N',
			input: '',
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			loginID: user.loginID,
			start: pullPage.start,
			limit: limit
		}
		var paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecQuery'
		}
		// console.log(JSON.stringify(pullPage));
		plus.nativeUI.showWaiting('正在获取...', {modal: false})
		$.DHCWebService(paramDict, function(result) {
			plus.nativeUI.closeWaiting();
			// console.log(JSON.stringify(result));
			var dataArr = result['data'];
			updateZhuiZongList(dataArr)
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
			pullPage.pulling = false;
			pullPage.pullObj.endPullUpToRefresh()
		});
	}
	
	// 渲染数据
	function updateZhuiZongList(dataArr){
		var ul = document.getElementById('OA_task_1');
		if(pullPage.start == 0 && dataArr.length == 0){
			// 这里就不再重复显示‘没有更多数据了’
			pullPage.pullObj.options.up.contentnomore = ''
			showNullTip('暂时没有内容，去其他地方看看吧！')
		}else{
			hideNullTip()
		}
		
		for (var i=0;i<dataArr.length;i++) {
			var one = dataArr[i];
			var src = one.NoticeFileUrl || 'http://iph.href.lu/400x300?text=没有配图';
			var liDom = document.createElement('li')
			ul.appendChild(liDom)
			liDom.setAttribute('class','mui-table-view-cell mui-card')
			liDom.innerHTML = '<div class="time">'+ one.date + '</div>'+'<div class="content">'+one.title+'</div>'+
				'<div class="readBtn" data-id="'+one.rw+'">标为已读</div>';
			liDom.querySelector('.readBtn').addEventListener('tap',readBtnTapped)
			liDom = null
		}
		pullPage.pulling = false;
		pullPage.start += dataArr.length;
		
		// console.log(pullPage.start);
		if(dataArr.length >= limit){
			pullPage.pullObj.endPullUpToRefresh()
		}else{
			pullPage.pullObj.endPullUpToRefresh(true)
		}
		ul = null
	}
	function readBtnTapped(){
		var rws = this.getAttribute('data-id')
		var pdict = {'rws':rws}
		var paramDict = {
			'method': 'web.INMPlatform:MarkReadMessage',
			'params': JSON.stringify(pdict),
			'type': 'Method'
		}
		//console.log(JSON.stringify(paramDict));
		plus.nativeUI.showWaiting('正在标记...', {modal: false})
		$.DHCWebService(paramDict, function(result) {
			plus.nativeUI.closeWaiting();
			//console.log(JSON.stringify(result));
			if (result.msg == '1') { //标记成功
				refreshMainPage()
				mui.toast('标记成功')
			}else{
				mui.toast(result.msg)
			}
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
			mui.toast('errorStr')
		});
	}
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		// console.log(index)
		mui('.nullData')[0].style.display = 'none';
	}
	
	function refreshMainPage() {
		pullPage.start = 0;
		pullPage.pullObj.endPullUpToRefresh(true);
		$('#wrapper').scroll().scrollTo(0,0,100);
		mui('#OA_task_1 .readBtn').each(function (index,element) {
			element.removeEventListener('tap',readBtnTapped)
		})
		$('#wrapper ul')[0].innerHTML = ''
		pullPage.pullObj.refresh(true);
		requestData()
	}
	
	var AnnouceAPI = {};
	AnnouceAPI.outRefresh = refreshMainPage; // 让外部接口等于内部接口 直接接受入参
	return AnnouceAPI
})(mui);

function refreshData(index){
	// console.log(index)
	AnnouceAPI.outRefresh() // true表示重置页面数据，从第0页开始查起
}